package com.cskaoyan.service.impl;

import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.po.MarketAddress;
import com.cskaoyan.bean.po.MarketAddressExample;
import com.cskaoyan.bean.usersadmin.AddressData;
import com.cskaoyan.mapper.MarketAddressMapper;
import com.cskaoyan.service.AddressService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ClassName: AddressServiceImpl
 * Description:
 * date: 2022/6/5 15:32
 *
 * @author bruce
 * @since JDK 8
 */
@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    MarketAddressMapper marketAddressMapper;

    @Override
    public AddressData query(BaseParam param, String name, Integer userId) {
        //开启分页
        PageHelper.startPage(param.getPage(), param.getLimit());
        MarketAddressExample example = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = example.createCriteria();
        //添加条件
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        criteria.andDeletedEqualTo(false);
        //排序
        example.setOrderByClause("add_time desc");
        List<MarketAddress> marketAddresses = marketAddressMapper.selectByExample(example);
        //获得分页信息
        PageInfo pageInfo = new PageInfo(marketAddresses);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return AddressData.data(total, pages, param.getLimit(), param.getPage(), marketAddresses);
    }
}
